package com.dykj.fireFighting.web.base.analysis.controller;

import com.dykj.fireFighting.api.base.analysis.dto.ComprehensiveQueryDto;
import com.dykj.fireFighting.api.base.devHisResult.vo.ResultVo;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.maintenance.service.IUrgentMaintenanceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.DateBetweenUtil;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 综合查询-隐患数据
 */
@Slf4j
@Api(value = "隐患数据", tags = {"隐患数据接口"})
@RestController
@RequestMapping("/zonghedanger")
public class ZongHeDangerController {

    /**
     * 单位（监控中心）
     */
    @DubboReference
    private IEnterpriseService enterpriseService;
    /**
     * 紧急维保
     */
    @DubboReference
    private IUrgentMaintenanceService urgentMaintenanceService;


    /**
     * 社会单位隐患排行
     *
     * @param comprehensiveQueryDto
     * @return
     * @throws ParseException
     */
    @AutoLog(value = "社会单位隐患排行")
    @ApiOperation(value = "社会单位隐患排行", notes = "社会单位隐患排行")
    @RequestMapping(value = "/rankingEcharts", method = RequestMethod.GET)
    public CommonResult getAllUnitDangerRank(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        List<ResultVo> socreVoList = new ArrayList<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
            monthList = DateBetweenUtil.getYearBetween(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
            monthList = DateBetweenUtil.getSeasonList(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        } else {
            monthList = DateBetweenUtil.getMonthBetween(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        }
        String start = comprehensiveQueryDto.getBeginTime() + " 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";

        //获取数据
        for (int i = 0; i < monthList.size(); i++) {
            if (i != 0) {
                start = monthList.get(i).substring(0, 7) + "-01 00:00:00";
            }

            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = monthList.get(i).substring(0, 4);
                String endYear = comprehensiveQueryDto.getEndTime().substring(0, 4);
                if (Integer.parseInt(year) < Integer.parseInt(endYear)) {
                    end = year + "-12-31 23:59:59";
                }
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = monthList.get(i).substring(0, 4);
                String month = monthList.get(i).substring(5, 7);
                int quarter = (Integer.parseInt(month) + 2) / 3;
                if (quarter == 1) {
                    end = year + "-03-31 23:59:59";
                } else if (quarter == 2) {
                    end = year + "-06-30 23:59:59";
                } else if (quarter == 3) {
                    end = year + "-09-30 23:59:59";
                } else if (quarter == 4) {
                    end = year + "-12-31 23:59:59";
                }
            } else {
                end = monthList.get(i) + " 23:59:59";
            }
            // 全部
            List<ResultVo> monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);
            socreVoList.addAll(monthSocreVoList);
        }


        //list排序
        //根据某个属性分组，汇总某个属性
        Map<String, IntSummaryStatistics> resultMap = new HashMap<>();
        if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
            resultMap = socreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
        } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
            resultMap = socreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
        } else {
            resultMap = socreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
        }

        List<ResultVo> resultList = new ArrayList<>();
        resultMap.forEach((key, value) -> {
            ResultVo result = new ResultVo();
            result.setName(key);
            result.setTotalScore((int) Math.round(value.getAverage()));
            resultList.add(result);
        });

        //list排序
        Integer[] arr = {1};
        List<ResultVo> returnSocreList = resultList.stream().sorted(Comparator.comparing(ResultVo::getTotalScore).reversed())
                .peek(e -> e.setRanking(arr[0]++)).collect(Collectors.toList());

        //split page
        long page = comprehensiveQueryDto.getPage();
        long limit = 20;
        List<ResultVo> subList = returnSocreList.stream().skip((page - 1) * limit).limit(limit).collect(Collectors.toList());
        PageVo<ResultVo> pageVo = new PageVo<>();
        pageVo.setList(subList);
        pageVo.setTotalCount(returnSocreList.size());
        return CommonResult.success(pageVo);
    }


    /**
     * echarts 趋势变化图
     *
     * @param comprehensiveQueryDto
     * @return
     * @throws Exception
     */
    @GetMapping("/trendEcharts")
    public CommonResult getAreaEchartsData(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, List<Double>> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }
        //时间段月份
        List<String> monthList = new ArrayList<>();
        if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
            monthList = DateBetweenUtil.getYearBetween(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
            monthList = DateBetweenUtil.getSeasonList(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        } else {
            monthList = DateBetweenUtil.getMonthBetween(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        }
        String start = comprehensiveQueryDto.getBeginTime() + " 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        //获取数据
        for (int i = 0; i < monthList.size(); i++) {
            if (i != 0) {
                start = monthList.get(i).substring(0, 7) + "-01 00:00:00";
            }

            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = monthList.get(i).substring(0, 4);
                String endYear = comprehensiveQueryDto.getEndTime().substring(0, 4);
                if (Integer.parseInt(year) < Integer.parseInt(endYear)) {
                    end = year + "-12-31 23:59:59";
                }
                monthList.set(i, year);
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = monthList.get(i).substring(0, 4);
                String month = monthList.get(i).substring(5, 7);
                int quarter = (Integer.parseInt(month) + 2) / 3;
                if (quarter == 1) {
                    end = year + "-03-31 23:59:59";
                } else if (quarter == 2) {
                    end = year + "-06-30 23:59:59";
                } else if (quarter == 3) {
                    end = year + "-09-30 23:59:59";
                } else if (quarter == 4) {
                    end = year + "-12-31 23:59:59";
                }
                monthList.set(i, year + "-" + quarter);
            } else {
                end = monthList.get(i) + " 23:59:59";
                monthList.set(i, monthList.get(i).substring(0, 7));
            }

            // 全部
            List<ResultVo> monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

            //list排序
            //根据某个属性分组，汇总某个属性
            Map<String, LongSummaryStatistics> monthResult = new HashMap<>();
            if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingLong(ResultVo::getNum)));
            } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingLong(ResultVo::getNum)));
            } else {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingLong(ResultVo::getNum)));
            }
            for (Map.Entry<String, LongSummaryStatistics> entry : monthResult.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    List<Double> resultList = resultMap.get(entry.getKey());
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    resultList.add(three);
                    resultMap.put(entry.getKey(), resultList);
                } else {
                    List<Double> resultList = new ArrayList<>();
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    resultList.add(three);
                    resultMap.put(entry.getKey(), resultList);
                }
            }

        }
        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }

    /**
     * 占比
     *
     * @param comprehensiveQueryDto
     * @return
     * @throws ParseException
     */
    @AutoLog(value = "社会单位隐患占比")
    @ApiOperation(value = "社会单位隐患占比", notes = "社会单位隐患占比")
    @RequestMapping(value = "/ratioEcharts", method = RequestMethod.GET)
    public CommonResult getAratioEcharts(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        List<ResultVo> socreVoList = new ArrayList<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
            monthList = DateBetweenUtil.getYearBetween(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
            monthList = DateBetweenUtil.getSeasonList(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        } else {
            monthList = DateBetweenUtil.getMonthBetween(comprehensiveQueryDto.getBeginTime(), comprehensiveQueryDto.getEndTime());
        }
        String start = comprehensiveQueryDto.getBeginTime() + " 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";

        //获取数据
        for (int i = 0; i < monthList.size(); i++) {
            if (i != 0) {
                start = monthList.get(i).substring(0, 7) + "-01 00:00:00";
            }

            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = monthList.get(i).substring(0, 4);
                String endYear = comprehensiveQueryDto.getEndTime().substring(0, 4);
                if (Integer.parseInt(year) < Integer.parseInt(endYear)) {
                    end = year + "-12-31 23:59:59";
                }
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = monthList.get(i).substring(0, 4);
                String month = monthList.get(i).substring(5, 7);
                int quarter = (Integer.parseInt(month) + 2) / 3;
                if (quarter == 1) {
                    end = year + "-03-31 23:59:59";
                } else if (quarter == 2) {
                    end = year + "-06-30 23:59:59";
                } else if (quarter == 3) {
                    end = year + "-09-30 23:59:59";
                } else if (quarter == 4) {
                    end = year + "-12-31 23:59:59";
                }
            } else {
                end = monthList.get(i) + " 23:59:59";
            }
            // 全部
            List<ResultVo> monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);
            socreVoList.addAll(monthSocreVoList);
        }


        //list排序
        //根据某个属性分组，汇总某个属性
        Map<String, IntSummaryStatistics> resultMap = new HashMap<>();
        if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
            resultMap = socreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
        } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
            resultMap = socreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
        } else {
            resultMap = socreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
        }

        List<ResultVo> resultList = new ArrayList<>();
        resultMap.forEach((key, value) -> {
            ResultVo result = new ResultVo();
            result.setName(key);
            result.setTotalScore((int) Math.round(value.getAverage()));
            resultList.add(result);
        });
        return CommonResult.success(resultList);
    }

    /**
     * 总数居对比
     *
     * @param comprehensiveQueryDto
     * @return
     */
    @ApiOperation(value = "同比 总数对比", notes = "同比 总数对比")
    @GetMapping("/getTongBiSum")
    public CommonResult getTongBiSum(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, Double> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        monthList.add(comprehensiveQueryDto.getBeginTime());
        monthList.add(comprehensiveQueryDto.getEndTime());

        String start = comprehensiveQueryDto.getBeginTime() + " 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        String key = comprehensiveQueryDto.getBeginTime();
        //获取数据
        for (int i = 0; i < monthList.size(); i++) {
            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = monthList.get(i).substring(0, 4);
                start = year + "-01-01 00:00:00";
                end = year + "-12-31 23:59:59";
                key = year + "年";
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = monthList.get(i).substring(0, 4);
                String quarter = monthList.get(i).substring(5, 6);
                if ("1".equals(quarter)) {
                    start = year + "-01-01 00:00:00";
                    end = year + "-03-31 23:59:59";
                    key = year + "年第一季度";
                } else if ("2".equals(quarter)) {
                    start = year + "-04-01 00:00:00";
                    end = year + "-06-30 23:59:59";
                    key = year + "年第二季度";
                } else if ("3".equals(quarter)) {
                    start = year + "-07-01 00:00:00";
                    end = year + "-09-30 23:59:59";
                    key = year + "年第三季度";
                } else if ("4".equals(quarter)) {
                    start = year + "-10-01 00:00:00";
                    end = year + "-12-31 23:59:59";
                    key = year + "年第四季度";
                }
            } else {
                String year = monthList.get(i).substring(0, 4);
                String month = monthList.get(i).substring(5, 7);
                LocalDate firstDayOfCurrentDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);
                LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                start = year + "-" + month + "-01 00:00:00";
                end = lastDayOfCurrentDate.toString() + " 23:59:59";
                key = year + "-" + month;
            }
            // 全部
            List<ResultVo> monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

            //list排序
            //根据某个属性分组，汇总某个属性
            Map<Integer, LongSummaryStatistics> monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getRanking, Collectors.summarizingLong(ResultVo::getNum)));
            for (Map.Entry<Integer, LongSummaryStatistics> entry : monthResult.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    Double resultList = resultMap.get(entry.getKey());
                    resultMap.put(key, resultList);
                } else {
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    resultMap.put(key, three);
                }
            }
        }
        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }

    /**
     * 详细数居对比
     *
     * @param comprehensiveQueryDto
     * @return
     */
    @ApiOperation(value = "同比 详细数据对比", notes = "同比 详细数据对比")
    @GetMapping("/getTongBiDetail")
    public CommonResult getTongBiDetail(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, List<String>> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        monthList.add(comprehensiveQueryDto.getBeginTime());
        monthList.add(comprehensiveQueryDto.getEndTime());

        String start = comprehensiveQueryDto.getBeginTime() + " 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        //获取数据
        List<ResultVo> monthSocreVoList = new ArrayList<>();
        for (String s : monthList) {
            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = s.substring(0, 4);
                for (int i = 1; i < 13; i++) {
                    LocalDate firstDayOfCurrentDate = LocalDate.of(Integer.parseInt(year), i, 1);
                    LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                    start = year + "-" + UUIDUtils.int2str(i) + "-01 00:00:00";
                    end = lastDayOfCurrentDate.toString() + " 23:59:59";
                    monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

                    //list排序
                    //根据某个属性分组，汇总某个属性
                    Map<Integer, IntSummaryStatistics> monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getRanking, Collectors.summarizingInt(ResultVo::getNum)));
                    for (Map.Entry<Integer, IntSummaryStatistics> entry : monthResult.entrySet()) {
                        if (resultMap.containsKey(String.valueOf(i))) {
                            List<String> resultList = resultMap.get(String.valueOf(i));

                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                            resultList.add(String.valueOf(three));
                            resultMap.put(String.valueOf(i), resultList);
                        } else {
                            List<String> resultList = new ArrayList<>();
                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            resultList.add(String.valueOf(three));
                            resultMap.put(String.valueOf(i), resultList);
                        }
                    }
                }
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                int year = Integer.parseInt(s.substring(0, 4));
                int month = 1;
                String quarter = s.substring(5, 6);
                if ("1".equals(quarter)) {
                    month = 1;
                } else if ("2".equals(quarter)) {
                    month = 4;
                } else if ("3".equals(quarter)) {
                    month = 7;
                } else if ("4".equals(quarter)) {
                    month = 10;
                }

                for (int i = 0; i < 3; i++) {
                    int num = month + i;

                    LocalDate firstDayOfCurrentDate = LocalDate.of(year, num, 1);
                    LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                    start = year + "-" + UUIDUtils.int2str(num) + "-01 00:00:00";
                    end = lastDayOfCurrentDate.toString() + " 23:59:59";
                    monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

                    //list排序
                    //根据某个属性分组，汇总某个属性
                    Map<Integer, IntSummaryStatistics> monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getRanking, Collectors.summarizingInt(ResultVo::getNum)));
                    for (Map.Entry<Integer, IntSummaryStatistics> entry : monthResult.entrySet()) {
                        if (resultMap.containsKey(String.valueOf(num))) {
                            List<String> resultList = resultMap.get(String.valueOf(num));

                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                            resultList.add(String.valueOf(three));
                            resultMap.put(String.valueOf(num), resultList);
                        } else {
                            List<String> resultList = new ArrayList<>();
                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            resultList.add(String.valueOf(three));
                            resultMap.put(String.valueOf(num), resultList);
                        }
                    }
                }
            } else {
                int year = Integer.parseInt(s.substring(0, 4));
                int month = Integer.parseInt(s.substring(5, 7));
                LocalDate firstDayOfCurrentDate = LocalDate.of(year, month, 1);
                LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                start = year + "-" + UUIDUtils.int2str(month) + "-01 00:00:00";
                end = lastDayOfCurrentDate.toString() + " 23:59:59";
                monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

                //list排序
                //根据某个属性分组，汇总某个属性
                Map<Integer, IntSummaryStatistics> monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getRanking, Collectors.summarizingInt(ResultVo::getNum)));
                for (Map.Entry<Integer, IntSummaryStatistics> entry : monthResult.entrySet()) {
                    if (resultMap.containsKey(entry.getKey())) {
                        List<String> resultList = resultMap.get(entry.getKey());

                        double one = entry.getValue().getAverage();
                        BigDecimal two = new BigDecimal(one);
                        double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                        resultList.add(String.valueOf(three));
                        resultMap.put(s, resultList);
                    } else {
                        List<String> resultList = new ArrayList<>();

                        double one = entry.getValue().getAverage();
                        BigDecimal two = new BigDecimal(one);
                        double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                        resultList.add(String.valueOf(three));
                        resultMap.put(s, resultList);
                    }
                }
            }
        }

        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }

    /**
     * 区域总数居对比
     *
     * @param comprehensiveQueryDto
     * @return
     */
    @ApiOperation(value = "同比 区域总数居对比", notes = "同比 区域总数居对比")
    @GetMapping("/getTongBiTypeSum")
    public CommonResult getTongBiTypeSum(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, List<String>> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        monthList.add(comprehensiveQueryDto.getBeginTime());
        monthList.add(comprehensiveQueryDto.getEndTime());

        String start = comprehensiveQueryDto.getBeginTime() + "-01 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        //获取数据
        List<ResultVo> monthSocreVoList = new ArrayList<>();
        for (String s : monthList) {
            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = s.substring(0, 4);
                start = year + "-01-01 00:00:00";
                end = year + "-12-31 23:59:59";
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = s.substring(0, 4);
                String quarter = s.substring(5, 6);
                if ("1".equals(quarter)) {
                    start = year + "-01-01 00:00:00";
                    end = year + "-03-31 23:59:59";
                } else if ("2".equals(quarter)) {
                    start = year + "-04-01 00:00:00";
                    end = year + "-06-30 23:59:59";
                } else if ("3".equals(quarter)) {
                    start = year + "-07-01 00:00:00";
                    end = year + "-09-30 23:59:59";
                } else if ("4".equals(quarter)) {
                    start = year + "-10-01 00:00:00";
                    end = year + "-12-31 23:59:59";
                }
            } else {
                int year = Integer.parseInt(s.substring(0, 4));
                int month = Integer.parseInt(s.substring(5, 7));
                LocalDate firstDayOfCurrentDate = LocalDate.of(year, month, 1);
                LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                start = year + "-" + UUIDUtils.int2str(month) + "-01 00:00:00";
                end = lastDayOfCurrentDate.toString() + " 23:59:59";
            }
            monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

            //根据某个属性分组，汇总某个属性
            Map<String, IntSummaryStatistics> monthResult = new HashMap<>();
            if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
            } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
            } else {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
            }
            for (Map.Entry<String, IntSummaryStatistics> entry : monthResult.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    List<String> resultList = resultMap.get(entry.getKey());
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    resultList.add(String.valueOf(three));
                    resultMap.put(entry.getKey(), resultList);
                } else {
                    List<String> resultList = new ArrayList<>();
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                    resultList.add(String.valueOf(three));
                    resultMap.put(entry.getKey(), resultList);
                }
            }
        }

        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }

    /**
     * 区域详细数据对比
     *
     * @param comprehensiveQueryDto
     * @return
     */
    @ApiOperation(value = "同比 区域详细数据对比", notes = "同比 区域详细数据对比")
    @GetMapping("/getTongBiTypeDetail")
    public CommonResult getTongBiTypeDetail(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, List<String>> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        monthList.add(comprehensiveQueryDto.getBeginTime());
        monthList.add(comprehensiveQueryDto.getEndTime());

        String start = comprehensiveQueryDto.getBeginTime() + "-01 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        //获取数据
        List<ResultVo> monthSocreVoList = new ArrayList<>();
        for (String s : monthList) {
            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = s.substring(0, 4);
                for (int i = 1; i < 13; i++) {
                    LocalDate firstDayOfCurrentDate = LocalDate.of(Integer.parseInt(year), i, 1);
                    LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                    start = year + "-" + UUIDUtils.int2str(i) + "-01 00:00:00";
                    end = lastDayOfCurrentDate.toString() + " 23:59:59";
                    monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

                    //根据某个属性分组，汇总某个属性
                    Map<String, IntSummaryStatistics> monthResult = new HashMap<>();
                    if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
                        monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
                    } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
                        monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
                    } else {
                        monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
                    }
                    for (Map.Entry<String, IntSummaryStatistics> entry : monthResult.entrySet()) {
                        if (resultMap.containsKey(entry.getKey())) {
                            List<String> resultList = resultMap.get(entry.getKey());
                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                            resultList.add(String.valueOf(three));
                            resultMap.put(entry.getKey(), resultList);
                        } else {
                            List<String> resultList = new ArrayList<>();
                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            resultList.add(String.valueOf(three));
                            resultMap.put(entry.getKey(), resultList);
                        }
                    }
                }
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                int year = Integer.parseInt(s.substring(0, 4));

                int month = 1;
                String quarter = s.substring(5, 6);
                if ("1".equals(quarter)) {
                    month = 1;
                } else if ("2".equals(quarter)) {
                    month = 4;
                } else if ("3".equals(quarter)) {
                    month = 7;
                } else if ("4".equals(quarter)) {
                    month = 10;
                }

                for (int i = 0; i < 3; i++) {
                    int num = month + i;

                    LocalDate firstDayOfCurrentDate = LocalDate.of(year, num, 1);
                    LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                    start = year + "-" + UUIDUtils.int2str(num) + "-01 00:00:00";
                    end = lastDayOfCurrentDate.toString() + " 23:59:59";
                    monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

                    //根据某个属性分组，汇总某个属性
                    Map<String, IntSummaryStatistics> monthResult = new HashMap<>();
                    if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
                        monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
                    } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
                        monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
                    } else {
                        monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
                    }
                    for (Map.Entry<String, IntSummaryStatistics> entry : monthResult.entrySet()) {
                        if (resultMap.containsKey(entry.getKey())) {
                            List<String> resultList = resultMap.get(entry.getKey());
                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                            resultList.add(String.valueOf(three));
                            resultMap.put(entry.getKey(), resultList);
                        } else {
                            List<String> resultList = new ArrayList<>();
                            double one = entry.getValue().getAverage();
                            BigDecimal two = new BigDecimal(one);
                            double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            resultList.add(String.valueOf(three));
                            resultMap.put(entry.getKey(), resultList);
                        }
                    }
                }


            } else {
                int year = Integer.parseInt(s.substring(0, 4));
                int month = Integer.parseInt(s.substring(5, 7));
                LocalDate firstDayOfCurrentDate = LocalDate.of(year, month, 1);
                LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                start = year + "-" + UUIDUtils.int2str(month) + "-01 00:00:00";
                end = lastDayOfCurrentDate.toString() + " 23:59:59";
                monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

                //根据某个属性分组，汇总某个属性
                Map<String, IntSummaryStatistics> monthResult = new HashMap<>();
                if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
                    monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
                } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
                    monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
                } else {
                    monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
                }
                for (Map.Entry<String, IntSummaryStatistics> entry : monthResult.entrySet()) {
                    if (resultMap.containsKey(entry.getKey())) {
                        List<String> resultList = resultMap.get(entry.getKey());
                        double one = entry.getValue().getAverage();
                        BigDecimal two = new BigDecimal(one);
                        double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                        resultList.add(String.valueOf(three));
                        resultMap.put(entry.getKey(), resultList);
                    } else {
                        List<String> resultList = new ArrayList<>();
                        double one = entry.getValue().getAverage();
                        BigDecimal two = new BigDecimal(one);
                        double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        resultList.add(String.valueOf(three));
                        resultMap.put(entry.getKey(), resultList);
                    }
                }
            }
        }

        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }


    /**
     * 总数居环比
     *
     * @param comprehensiveQueryDto
     * @return
     */
    @ApiOperation(value = "环比 总数对比", notes = "环比 总数对比")
    @GetMapping("/getHuanBiSum")
    public CommonResult getHuanBiSum(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, Double> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        monthList.add(comprehensiveQueryDto.getBeginTime());
        monthList.add(comprehensiveQueryDto.getEndTime());

        String start = comprehensiveQueryDto.getBeginTime() + " 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        String key = comprehensiveQueryDto.getBeginTime();
        //获取数据
        for (int i = 0; i < monthList.size(); i++) {
            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = monthList.get(i).substring(0, 4);
                start = year + "-01-01 00:00:00";
                end = year + "-12-31 23:59:59";
                key = year + "年";
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = monthList.get(i).substring(0, 4);
                String quarter = monthList.get(i).substring(5, 6);
                if ("1".equals(quarter)) {
                    start = year + "-01-01 00:00:00";
                    end = year + "-03-31 23:59:59";
                    key = year + "年第一季度";
                } else if ("2".equals(quarter)) {
                    start = year + "-04-01 00:00:00";
                    end = year + "-06-30 23:59:59";
                    key = year + "年第二季度";
                } else if ("3".equals(quarter)) {
                    start = year + "-07-01 00:00:00";
                    end = year + "-09-30 23:59:59";
                    key = year + "年第三季度";
                } else if ("4".equals(quarter)) {
                    start = year + "-10-01 00:00:00";
                    end = year + "-12-31 23:59:59";
                    key = year + "年第四季度";
                }
            } else {
                String year = monthList.get(i).substring(0, 4);
                String month = monthList.get(i).substring(5, 7);
                LocalDate firstDayOfCurrentDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);
                LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                start = year + "-" + month + "-01 00:00:00";
                end = lastDayOfCurrentDate.toString() + " 23:59:59";
                key = year + "-" + month;
            }
            // 全部
            List<ResultVo> monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

            //list排序
            //根据某个属性分组，汇总某个属性
            Map<Integer, LongSummaryStatistics> monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getRanking, Collectors.summarizingLong(ResultVo::getNum)));
            for (Map.Entry<Integer, LongSummaryStatistics> entry : monthResult.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    Double resultList = resultMap.get(entry.getKey());
                    resultMap.put(key, resultList);
                } else {
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    resultMap.put(key, three);
                }
            }
        }
        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }

    /**
     * 区域总数居环比
     *
     * @param comprehensiveQueryDto
     * @return
     */
    @ApiOperation(value = "环比 区域总数居对比", notes = "环比 区域总数居对比")
    @GetMapping("/getHuanBiTypeSum")
    public CommonResult getHuanBiTypeSum(ComprehensiveQueryDto comprehensiveQueryDto) throws ParseException {
        Map<String, Object> resultData = new HashMap<>();
        Map<String, List<String>> resultMap = new HashMap<>();
        //监控中心查下辖的单位，社会单位查自己，管理员查全部社会单位
        List<Enterprise> list = new ArrayList<>();
        if (StringUtils.isBlank(comprehensiveQueryDto.getUnitIds())) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            list = enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        } else {
            String[] unitIds = comprehensiveQueryDto.getUnitIds().split(",");
            for (String unitId : unitIds) {
                Enterprise enterprise = enterpriseService.getById(unitId);
                list.add(enterprise);
            }
        }
        List<String> ids = new ArrayList<>();
        for (Enterprise enterprise : list) {
            ids.add(enterprise.getId() + "");
        }

        //时间段月份
        List<String> monthList = new ArrayList<>();
        monthList.add(comprehensiveQueryDto.getBeginTime());
        monthList.add(comprehensiveQueryDto.getEndTime());

        String start = comprehensiveQueryDto.getBeginTime() + "-01 00:00:00";
        String end = comprehensiveQueryDto.getEndTime() + " 23:59:59";
        //获取数据
        List<ResultVo> monthSocreVoList = new ArrayList<>();
        for (String s : monthList) {
            if ("1".equals(comprehensiveQueryDto.getTimeGranularity())) {
                String year = s.substring(0, 4);
                start = year + "-01-01 00:00:00";
                end = year + "12-31 23:59:59";
            } else if ("2".equals(comprehensiveQueryDto.getTimeGranularity())) {
                //月份对应季度
                String year = s.substring(0, 4);
                String quarter = s.substring(5, 6);
                if ("1".equals(quarter)) {
                    start = year + "-01-01 00:00:00";
                    end = year + "-03-31 23:59:59";
                } else if ("2".equals(quarter)) {
                    start = year + "-04-01 00:00:00";
                    end = year + "-06-30 23:59:59";
                } else if ("3".equals(quarter)) {
                    start = year + "-07-01 00:00:00";
                    end = year + "-09-30 23:59:59";
                } else if ("4".equals(quarter)) {
                    start = year + "-10-01 00:00:00";
                    end = year + "-12-31 23:59:59";
                }
            } else {
                int year = Integer.parseInt(s.substring(0, 4));
                int month = Integer.parseInt(s.substring(5, 7));
                LocalDate firstDayOfCurrentDate = LocalDate.of(year, month, 1);
                LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());

                start = year + "-" + UUIDUtils.int2str(month) + "-01 00:00:00";
                end = lastDayOfCurrentDate.toString() + " 23:59:59";
            }
            monthSocreVoList = urgentMaintenanceService.getTrendEcharts(start, end, ids);

            //根据某个属性分组，汇总某个属性
            Map<String, IntSummaryStatistics> monthResult = new HashMap<>();
            if ("1".equals(comprehensiveQueryDto.getDataDimension())) {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getUnitName, Collectors.summarizingInt(ResultVo::getNum)));
            } else if ("2".equals(comprehensiveQueryDto.getDataDimension())) {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getAreaName, Collectors.summarizingInt(ResultVo::getNum)));
            } else {
                monthResult = monthSocreVoList.stream().collect(Collectors.groupingBy(ResultVo::getIndustryName, Collectors.summarizingInt(ResultVo::getNum)));
            }
            for (Map.Entry<String, IntSummaryStatistics> entry : monthResult.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
                    List<String> resultList = resultMap.get(entry.getKey());
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    resultList.add(String.valueOf(three));
                    resultMap.put(entry.getKey(), resultList);
                } else {
                    List<String> resultList = new ArrayList<>();
                    double one = entry.getValue().getAverage();
                    BigDecimal two = new BigDecimal(one);
                    double three = two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                    resultList.add(String.valueOf(three));
                    resultMap.put(entry.getKey(), resultList);
                }
            }
        }

        resultData.put("month", monthList);
        resultData.put("data", resultMap);
        return CommonResult.success(resultData);
    }

}
